/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import controller.AddUser;
import controller.CheckUser;
import controller.EncryptMD5;
import controller.ListRoles;
import controller.RollDetail;
import entities.Role;
import entities.Users;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author QUY-THI
 */
@ManagedBean
@ViewScoped
public class AddUserBean {

    private String username;
    private String fullname;
    private String gender;
    private Date dateofbirth;
    private String phone;
    private String email;
    private String address;
    private int roleID;
    private List<Role> roles;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getDateofbirth() {
        return dateofbirth;
    }

    public void setDateofbirth(Date dateofbirth) {
        this.dateofbirth = dateofbirth;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getRoleID() {
        return roleID;
    }

    public void setRoleID(int roleID) {
        this.roleID = roleID;
    }

    public List<Role> getRoles() {
        ListRoles r = new ListRoles();
        return r.getLRoles();
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    /**
     * Creates a new instance of AddUserBean
     */
    public AddUserBean() {
    }

    public String resultAdd() {
        CheckUser check = new CheckUser();
        Users use = check.checkUser(username);
        if (use == null) {
            RollDetail role = new RollDetail();
            Role roleDetail = role.roleDetail(roleID);
            Users u = new Users();
            u.setRoleID(roleDetail);
            u.setUsername(username);
            u.setPassword(EncryptMD5.getHashString("123456"));
            u.setCreateDate(new Date());
            u.setFullname(fullname);
            u.setGender(gender);
            u.setBod(dateofbirth);
            u.setPhone(phone);
            u.setEmail(email);
            u.setAddress(address);
            u.setStatus(true);
            AddUser add = new AddUser();
            boolean addUser = add.addUser(u);
            if (addUser) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Success"));
                return "";
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Error please try again later"));
                return "";
            }
        } else {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Username already exists !"));
            return "";
        }
    }
}
